<script setup lang="ts">
import { onMounted, computed, ref, watch, nextTick } from 'vue'
import { Icons } from "@/constants/images";

const props = withDefaults(
    defineProps<{
        question: any; // 题目
        isright: boolean; // 是否正确
        isResult: boolean; // 是否是结果页
    }>(),
    {
        isright: false,
        isResult: false,
    }
);

const emit = defineEmits(["selectAnswer"]);

// 选中答案
const selectAnswer = (item: string) => {
    emit("selectAnswer", item);

};
// isSelected
const isSelected = (item: string) => {
    return props.question.userAnswer === item;
};


const isMobile = computed(() => {
    const viewportWidth = document.documentElement.clientWidth;
    return viewportWidth <= 768;
});
const duration = ref("00:00");
const progressTime = ref(0);


const secondsToMinutes = (seconds: any) => {
    // 计算分钟数  
    let minutes = Math.floor(seconds / 60);
    // 计算剩余的秒数  
    let remainingSeconds = seconds % 60;

    // 使用字符串的padStart方法来确保分钟和秒都是两位数  
    // '02'表示如果字符串长度小于2，则在前面填充'0'直到长度为2  
    let formattedMinutes = minutes.toString().padStart(2, '0');
    let formattedSeconds = remainingSeconds.toString().padStart(2, '0');

    // 返回结果  
    return `${formattedMinutes}:${formattedSeconds}`;
}

const myPropComputed = computed(() => {
    return props.question;
});

//@ts-ignore
watch(myPropComputed, (newVal, oldVal) => {
    nextTick(() => {
        const audioElement = document.getElementById('audioElement');
        if (!audioElement) {
            return;
        }
        // 当音频的元数据加载完成后（如时长、轨道数等），执行此函数  
        //@ts-ignore
        audioElement.addEventListener('loadedmetadata', function () {
            // 获取并显示音频时长

            //@ts-ignore

            var dura = Math.round(audioElement.duration);
            console.log(dura);
            duration.value = secondsToMinutes(dura);
        });
        //@ts-ignore
        audioElement.addEventListener('timeupdate', () => {
            //@ts-ignore
            var currentTime = audioElement.currentTime, duration = audioElement.duration;
            progressTime.value = currentTime / duration * 100;
        });
    })

});
onMounted(() => {

})
</script>

<template>
    <div class="question">
        <div class="top-box">
            <div>
                <div class="title">{{ props.question.title }}</div>
                <div class="describe" v-html="props.question.describe"></div>
            </div>
            <div class="audio-box" v-if="props.question.audioUrl">
                <audio id="audioElement" :src="props.question.audioUrl" controls="false" autoplay hidden="true"></audio>
                <img class="icon_voice_play play" :src="Icons.ICON_PLAY">
                <progress v-if="!isMobile" class="progress is-primary is-small" :value="progressTime" max="100" />
                <span>{{ duration }}</span>
            </div>
        </div>
        <div class="question-title my-5">
            <div style="margin-bottom: 20px;" v-if="props.question.hasOwnProperty('materials')" v-html="props.question.materials"></div>
            <div>
                <img v-if="props.question.hasOwnProperty('img')" :src="props.question.img" alt="img">
            </div>
            <span v-html="props.question.content"></span>
        </div>
        <div class="question-options">
            <div v-for="(item, index) in props.question.options" :key="index"
                @click="selectAnswer(String.fromCharCode(65 + index))" class="option my-4" :class="{
                    selected: isSelected(String.fromCharCode(65 + index)),
                    right:
                        isSelected(String.fromCharCode(65 + index)) && isright && isResult,
                    wrong:
                        isSelected(String.fromCharCode(65 + index)) && !isright && isResult,
                }">
                <div class="is-flex">
                    <span class="symbol">{{ String.fromCharCode(65 + index) }}</span>
                    <span>{{ item.opContent }}</span>
                </div>
                <span v-if="isSelected(String.fromCharCode(65 + index)) && isright && isResult
                    " class="icon">
                    <img :src="Icons.ICON_HOOK" />
                </span>
                <span v-if="isSelected(String.fromCharCode(65 + index)) && !isright && isResult
                    " class="icon">
                    <img :src="Icons.ICON_FORK" />
                </span>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.question {

    .top-box {
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 3px solid #F7F7F7;


        .audio-box {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .progress {
            margin-bottom: 0;
            width: 340px;
        }
    }

    .title {
        font-size: 24px;
        color: #141623;
        line-height: 22px;
        margin-bottom: 15px;

        &::before {
            content: "";
            display: inline-block;
            width: 6px;
            height: 14px;
            border-radius: 3px;
            background-color: #00CCCF;
            margin-right: 12px;
            margin-top: 2px;
        }
    }

    .describe {
        font-size: 24px;
        color: #14161E;
        line-height: 30px;
        padding-bottom: 30px;
        font-weight: 400;
    }

    .question-title {
        font-size: 18px;
        font-weight: 600;
    }

    .question-options {
        .option {
            padding: 30px;
            background-color: rgba($color: #000000, $alpha: 0.03);
            border-radius: 15px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            cursor: pointer;

            &.selected {
                background-color: #00CCCF;
                color: #fff;

                .symbol {
                    color: #fff;

                    &::after {
                        background-color: #fff;
                    }
                }
            }

            &.right {
                background-color: #00ac26 !important;
                color: #fff;

                .symbol {
                    color: #fff !important;

                    &::after {
                        background-color: #fff !important;
                    }
                }
            }

            &.wrong {
                background-color: #ff6464 !important;
                color: #fff;

                .symbol {
                    color: #fff !important;

                    &::after {
                        background-color: #fff !important;
                    }
                }
            }

            .symbol {
                font-size: 18px;
                font-weight: 600;
                padding-right: 25px;
                margin-right: 25px;
                color: #000;
                position: relative;
                display: flex;
                align-items: center;

                &::after {
                    content: "";
                    display: block;
                    width: 4px;
                    height: 16px;
                    border-radius: 2px;
                    background-color: #d0d0d0;
                    position: absolute;
                    right: 0;
                }
            }

            span {
                font-size: 1rem;
            }
        }
    }
}

@media (max-width: 768px) {
    .question {
        .title {
            font-size: 14px;
            color: #141623;
            line-height: 18px;
        }

        .title::before {
            content: none;
        }

        .describe {
            font-weight: 400;
            font-size: 14px;
            color: #9395A4;
            line-height: 18px;
            border-bottom: none;
            padding-bottom: 0;
        }

        .question-title {
            font-weight: 500;
            font-size: 16px;
            color: #141623;
            line-height: 24px;
        }

        .question-options {
            .option {
                padding: 12px 20px;
                margin: 12px 0;

                .symbol {
                    font-weight: 500;
                    font-size: 16px;
                    line-height: 20px;
                }

                .symbol::after {
                    width: 1px;
                }

                span {
                    font-weight: 400;
                    font-size: 14px;
                    line-height: 24px;
                }
            }
        }
    }
}
</style>
